package com.hxkj.controller;

import com.hxkj.domain.po.AllocationFunction;
import com.hxkj.domain.po.AuthRole;
import com.hxkj.service.AuthRoleService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * 角色控制器
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@RestController
@RequestMapping("/authrole")
public class AuthRoleController {

    @Autowired
    private AuthRoleService authRoleService;


    /**
     * 获取角色列表
     *
     * @param authRole 角色跟分页
     * @return 翻页数据
     * @throws Exception
     */
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    public Callable<ResultPageStruct<AuthRole>> getRoleList(@ModelAttribute AuthRole authRole, @ModelAttribute PageTo pageParam) throws Exception {
        return () -> authRoleService.getRoleList(authRole == null ? new AuthRole() : authRole,pageParam);
    }

    /**
     * 获取角色列表
     *
     * @param authRole 从从库获取角色列表
     * @return 翻页数据
     * @throws Exception
     */
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/getlist/slave", method = RequestMethod.POST)
    public Callable<ResultPageStruct<AuthRole>> getAuthRoleBySlave(@ModelAttribute AuthRole authRole, @ModelAttribute PageTo pageParam) throws Exception {
        return () ->authRoleService.getAuthRoleBySlave(authRole == null ? new AuthRole() : authRole,pageParam);
    }

    /**
     * 获取单一角色
     *
     * @param authRole 获取单个角色
     * @return 单个角色
     * @throws Exception
     */
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Callable<ResultDataStruct<AuthRole>> getAuthRole(@ModelAttribute AuthRole authRole) throws Exception {
        return () -> authRoleService.getAuthRole(authRole);
    }

    /**
     * 新增角色
     *
     * @param authRole 角色信息
     * @throws Exception
     */
    @RequiresPermissions("role:add")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultMessageStruct saveAuthRole(@ModelAttribute AuthRole authRole) throws Exception {
        return authRoleService.saveAuthRole(authRole);
    }

    /**
     * 删除角色
     *
     * @param authRole 角色信息
     * @throws Exception
     */
    @RequiresPermissions("role:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ResultMessageStruct deleteAuthRole(@ModelAttribute AuthRole authRole) throws Exception {
        return authRoleService.deleteAuthRole(authRole);
    }

    /**
     * 更新角色
     *
     * @param authRole 角色信息
     * @throws Exception
     */
    @RequiresPermissions("role:modify")
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ResultMessageStruct updateAuthRole(@ModelAttribute AuthRole authRole) throws Exception {
        return authRoleService.updateAuthRole(authRole);
    }

    /**
     * 获取该角色已分配的功能/全部功能
     *
     * @param authRole 角色
     * @return  已分配的功能/全部功能
     * @throws Exception
     */
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/getfunction", method = RequestMethod.POST)
    public Callable<AllocationFunction> getFunction(@ModelAttribute AuthRole authRole) throws Exception {
        return () -> authRoleService.getFunction(authRole);
    }
    /**
     * 获取该角色已分配的功能/全部功能
     *
     * @param authRole 角色
     * @return  已分配的功能/全部功能
     * @throws Exception
     */
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/getfunctions", method = RequestMethod.POST)
    public Callable<AllocationFunction> getFunctions(@ModelAttribute AuthRole authRole) throws Exception {
        return () -> authRoleService.getFunctions(authRole);
    }
    /**
     * 为该角色分配功能
     *
     * @param authRole 角色
     * @return  已分配的功能/全部功能
     * @throws Exception
     */
    @RequiresPermissions("role:add")
    @RequestMapping(value = "/setfunction", method = RequestMethod.POST)
    public Callable<ResultMessageStruct> allocationFunction(@ModelAttribute AuthRole authRole) throws Exception {
        return () -> authRoleService.saveAllocationFunction(authRole);
    }
}
